# -*- coding: utf-8 -*-
import math
import logging
from decimal import Decimal, ROUND_HALF_UP, getcontext

import py_global

logger = logging.getLogger(__name__)


def py_float():
    nan = float('nan')
    print(math.isnan(nan))

    # np.isnan(nan)
    # pd.isna(nan)

    inf = float('inf')
    print(math.isinf(inf))

    pass


def py_decimal():
    d: Decimal = Decimal.from_float(0.123456789)
    logger.info(f'Decimal:{d}')

    # 类型转换
    f = float(d)
    logger.info(f'float:{f}')
    # 四舍五入
    d1 = d.quantize(Decimal('0.0000'), rounding=ROUND_HALF_UP)
    logger.info(f'ROUND_HALF_UP:{d1}')

    d2: Decimal = Decimal('1234567890')
    i = int(d2)
    logger.info(f'int:{i}')


def compare():
    getcontext().prec = 2
    getcontext().rounding = ROUND_HALF_UP
    # float
    fa = 1.1
    fb = 2.2
    fc = 3.3
    logger.info(f'float:{fa + fb == fc}')

    # Decimal比较：比较不相等
    da = Decimal.from_float(fa)
    db = Decimal.from_float(fb)
    dc = Decimal.from_float(fc)
    dd = da + db
    logger.info(f'Decimal.from_float:{dd} == {dc} is {dd == dc}')

    # 只有这种情况比较相等
    a = Decimal('1.1')
    b = Decimal('2.2')
    c = Decimal('3.3')
    logger.info(f'Decimal:{a + b == c}')


if __name__ == '__main__':
    # py_decimal()

    # compare()
    # Decimal.from_float(0.1) / Decimal.from_float(0)

    logger.info(Decimal("0.0") == 0.0)
    logger.info(Decimal("0.0") == 0)
    logger.info(Decimal.from_float(0) == 0.0)
    if Decimal("0.0"):
        logger.info("0.0")
    pass
